package net.libra.scrollui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import net.libra.ui.EasyButton;
	import net.libra.ui.base.BaseSprite;
	import net.libra.ui.layer.LayerManager;

	/**
	 *@author elvisjiang
	 *@createTime 2011-7-14
	 */
	public class VSlider extends BaseSprite
	{
		private var _button:EasyButton;
		private var _skin:DisplayObjectContainer;
		
		private var _trackHeight:Number = 0;//轨道长
		//滚动条比例率
		public function VSlider(skin:DisplayObjectContainer,button:EasyButton,trackHeight:Number)
		{
			this.mouseChildren = true;
			_skin = skin;
			_button = button;
			_trackHeight = trackHeight;
			initDisplay();
			initEvent();
		}
		protected function initDisplay():void
		{
			this.addChild(_skin);
			_skin.addChild(_button);
		}
		protected function initEvent():void
		{
			_button.addEventListener(MouseEvent.MOUSE_DOWN,button_MouseDownHandler);
		}
		private var mouseDownY:Number;
		private function button_MouseDownHandler(e:MouseEvent):void
		{
			mouseDownY = e.localY;
			LayerManager.getStage().addEventListener(MouseEvent.MOUSE_MOVE,stage_MouseMoveHander);
			LayerManager.getStage().addEventListener(MouseEvent.MOUSE_UP,stage_MouseUpHandler);
			_button.startDrag(false,new Rectangle(-3,0,0,_trackHeight - _button.height));
		}
		protected function stage_MouseUpHandler(e:MouseEvent):void
		{
			_button.stopDrag();
//			trace("this.button.y:",this.button.y);
			LayerManager.getStage().removeEventListener(MouseEvent.MOUSE_UP,stage_MouseUpHandler);
			LayerManager.getStage().removeEventListener(MouseEvent.MOUSE_MOVE,stage_MouseMoveHander);
		}
		public var buttontempY:Number = 0;
		public var theEndScroll:Boolean = true;
		/**
		 * 连续的值，需要比率系数的话，到用到scollerBar的类里设定，然后用this.value*offPercent
		 */ 
		protected function stage_MouseMoveHander(e:MouseEvent):void
		{
			if(button.y < 0){
				return;
			}
			if(button.y + button.height >= trackHeight){
				button.y = trackHeight - button.height;
				dispatchEvent(new ListEvent(ListEvent.SCROLL,button.y));
				theEndScroll = false;//拖到到最后只发一次.
				return;
			}
			
			if(button.y != buttontempY){
				dispatchEvent(new ListEvent(ListEvent.SCROLL,button.y));				
			}
			buttontempY = button.y;
		}
		public function get buttonMaxPostion():Boolean
		{
			if(button.y + button.height >= trackHeight)
			{
				return true;
			}
			return false;
		}
		public function setButtonMaxPostion():void
		{
			button.y = trackHeight - button.height; 
		}
		public function set buttonHeight(value:Number):void
		{
			_button.skin.height = uint(value);
		}
		override protected function render():void
		{
			
		}
		public function get button():EasyButton
		{
			return _button;
		}
		public function isEnabedScroll():Boolean
		{
			return _button.visible;
		}
		public function get trackHeight():Number
		{
			return _trackHeight;
		}
		override public function destroy():void
		{
			_button.removeEventListener(MouseEvent.MOUSE_DOWN,button_MouseDownHandler);
			_skin.removeChild(_button);
			_button.destory();
			_button = null;
			super.destroy();
		}
	}
}